/**
 * Copyright (C) 2008-2009 Cristian Olaru <colaru@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.integrationpath.mengine.webapp.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.ServletContext;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.struts2.ServletActionContext;

import com.integrationpath.mengine.webapp.listener.StartupListener;
import com.integrationpath.mengine.webapp.util.EngineConstants;
import com.opensymphony.xwork2.Preparable;

public class ConfigurationAction extends BaseAction implements Preparable {
	private HashMap configurations;
	private String applicationTheme;

	public String getApplicationTheme() {
		return applicationTheme;
	}

	public void setApplicationTheme(String applicationTheme) {
		this.applicationTheme = applicationTheme;
	}

	public HashMap getConfigurations() {
		return configurations;
	}

	public void setConfigurations(HashMap configurations) {
		this.configurations = configurations;
	}

	/**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        	Configuration configuration = (Configuration)ServletActionContext.getServletContext().getAttribute(EngineConstants.CONFIG_KEY);   
    		configurations = new HashMap();
        	Iterator iterator = configuration.getKeys();
        	while (iterator.hasNext()) {
				String key = (String) iterator.next();
				String property = configuration.getString(key);
				if(!key.endsWith("title&"))
					configurations.put(key, property);
			}
        
    }

    public String delete() {
        return SUCCESS;
    }

    public String edit() {
    	PropertiesConfiguration configuration = (PropertiesConfiguration)ServletActionContext.getServletContext().getAttribute(EngineConstants.CONFIG_KEY);
    	Set keySet = configurations.keySet();
    	Iterator iterator = keySet.iterator();
    	while (iterator.hasNext()) {
			String key = (String) iterator.next();
			String[] properties = (String[]) configurations.get(key);		
			configuration.setProperty(key, properties[0]);
		}
    	try {
			configuration.save();
		} catch (ConfigurationException e) {
			log.error("Error in saving application configuration", e);
		}
		
		String[] properties = (String[]) configurations.get("application.current.theme");
		ServletContext servletContext = StartupListener.getAppContext();
		servletContext.setAttribute("application_theme", configuration.getProperty("application.current.theme") + properties[0]);
		servletContext.setAttribute("path_theme", "/themes/" + properties[0].toString().toLowerCase());
		
		 setApplicationTheme((String)configuration.getProperty("application.current.theme"));
		
        return SUCCESS;
    }
    
    public String applicationConfiguration() throws Exception {
		
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }
            return SUCCESS;
    }
}